﻿package mortal.component.ui
{
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.manager.*;
    import extend.language.*;
    import flash.text.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class MoneyBox extends ToolTipSprite
    {
        private var _title:GTextFiled;
        private var _moneyTxt:GTextFiled;
        private var _icon:GBitmap;
        private var _moneyType:int;
        private var _label:String;
        private var _autoUpdate:Boolean;

        public function MoneyBox()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseChildren = false;
            this.mouseEnabled = true;
            this._title = UIFactory.gTextField("", 0, 0, 0, 20, this);
            this._title.autoSize = TextFieldAutoSize.LEFT;
            this._title.wordWrap = true;
            this._title.multiline = true;
            this._moneyTxt = UIFactory.gTextField("2", 0, 0, 0, 20, this, GlobalStyle.textFormatBai);
            this._moneyTxt.autoSize = TextFieldAutoSize.LEFT;
            this._moneyTxt.wordWrap = true;
            this._moneyTxt.multiline = true;
            this._icon = UIFactory.gBitmap("", 0, 0, this);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            this._title.dispose(param1);
            this._title = null;
            this._moneyTxt.dispose(param1);
            this._moneyTxt = null;
            this._icon.dispose(param1);
            this._icon = null;
            this._autoUpdate = false;
            super.disposeImpl(param1);
            return;
        }// end function

        public function set autoUpdate(param1:Boolean) : void
        {
            this._autoUpdate = param1;
            if (this._autoUpdate)
            {
                NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            }
            else
            {
                NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            }
            return;
        }// end function

        private function updateMoney(param1:Object = null) : void
        {
            if (this._label == "")
            {
                this._title.htmlText = Language.getStringByParam(20371, GameDefConfig.instance.getEPrictUnitName(this._moneyType));
            }
            else if (this._label == null)
            {
                this._title.htmlText = GameDefConfig.instance.getEPrictUnitName(this._moneyType) + " : ";
            }
            else
            {
                this._title.htmlText = this._label + GameDefConfig.instance.getEPrictUnitName(this._moneyType) + " : ";
            }
            this._moneyTxt.text = Cache.instance.role.getMoneyByType(this._moneyType).toString();
            this.toolTipData = GameDefConfig.instance.getEPrictUnitName(this._moneyType);
            this.resetPosition();
            return;
        }// end function

        public function setMoney(param1:int, param2:String = "") : void
        {
            this._label = param2;
            this.setMoneyType(param1);
            this.updateMoney();
            return;
        }// end function

        public function get text() : String
        {
            return this._moneyTxt.text;
        }// end function

        public function setMoneyType(param1:int) : void
        {
            this._moneyType = param1;
            this._icon.bitmapData = GlobalClass.getBitmapData(GameDefConfig.instance.getEPrictUnitImg(this._moneyType));
            return;
        }// end function

        public function resetMoney(param1:int) : void
        {
            this._moneyTxt.text = param1.toString();
            this.resetPosition();
            return;
        }// end function

        private function resetPosition() : void
        {
            this._moneyTxt.x = this._title.x + this._title.textWidth + 5;
            this._icon.x = this._moneyTxt.x + this._moneyTxt.textWidth + 5;
            return;
        }// end function

    }
}
